package com.cloud.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.cloud.admin.mapper.SysUserRoleMapper;
import com.cloud.admin.model.entity.SysUserRole;
import com.cloud.admin.service.SysUserRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户和角色关联表
 *
 * @author douhaichao code generator
 * @date 2019-07-09 10:26:57
 */
@Service
@AllArgsConstructor
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public SysUserRole selectOne(SysUserRole req) {
        QueryWrapper queryWrapper = new QueryWrapper<SysUserRole>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<SysUserRole> selectAll(SysUserRole req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<SysUserRole> selectPage(Page page, SysUserRole req) {
        IPage<SysUserRole> list = baseMapper.selectPage(
                page,
                new QueryWrapper<SysUserRole>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<SysUserRole> selectPageByLike(Page page, SysUserRole req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (null != req.getRoleId()) {
            queryWrapper.eq("role_id", req.getRoleId());
        }
        IPage<SysUserRole> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(SysUserRole req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }
}
